home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Classes / RCString / listtester.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  101 lines

  1. //
  2. // testing of NeXT style object archiving and
  3. // unarchiving.
  4. //
  5. #import <stdio.h>
  6. #import <stdlib.h>
  7. #import <libc.h>
  8.  
  9. #import <objc/List.h>
  10.  
  11. #import "RCString.h"
  12.  
  13. int
  14. main(int c, char **v)
  15. {
  16.     char            buf[1024];
  17.     RCString       *oBject;
  18.     List           *oList, *oList2;
  19.     int             icObject = c, j;
  20.     id              idSomething;
  21.     NXStream       *spStream;
  22.     NXTypedStream  *spTypedStream;
  23.  
  24.     gets(buf);
  25.  
  26.     [(oList = [List alloc]) init];
  27.  
  28.     if (oList == NULL) {
  29.         fprintf(stderr, "puked allocating List object\n");
  30.         exit(19);
  31.     }
  32.  
  33.     while (c--) {
  34.         oBject = [RCString newFromString:v[c]];
  35.         idSomething = [oList addObject:oBject];
  36.         printf("added an RCString, \"%s\", 0x%x, %d\n",
  37.             [oBject data], [oBject data], [oBject references]);
  38.         idSomething = [oBject newFromObject];
  39.         [oList addObject:idSomething];
  40.         printf("added an RCString, \"%s\", 0x%x, %d\n",
  41.             [idSomething data], [idSomething data], [idSomething references]);
  42.     }
  43.  
  44.  
  45.     printf("Should be %d objects in List, there are %d\n",
  46.         icObject, [oList count]);
  47.  
  48.     gets(buf);
  49.  
  50.     printf("writing whole list to \"%s\"\n", *(v + 1));
  51.  
  52.     spTypedStream = NXOpenTypedStreamForFile(*(v + 1), NX_WRITEONLY);
  53.     if (spTypedStream == NULL) {
  54.         fprintf(stderr, "hosed opening typed stream for \"%s\"\n",
  55.         *(v + 1));
  56.         [[oList freeObjects] free];
  57.         exit(13);
  58.     }
  59.  
  60.     NXWriteRootObject(spTypedStream, oList);
  61.  
  62.     printf("wrote all objects.\n");
  63.  
  64.     NXCloseTypedStream(spTypedStream);
  65.  
  66.     printf("reading objects back in.\n");
  67.  
  68.     spStream = NXMapFile(*(v + 1), NX_READONLY);
  69.     if (spStream) {
  70.         spTypedStream = NXOpenTypedStream(spStream, NX_READONLY);
  71.         if (spTypedStream == NULL) {
  72.             fprintf(stderr, "couldn't convert to typedstream\n");
  73.         } else {
  74.             oList2 = NXReadObject(spTypedStream);
  75.             if (oList2 == NULL) {
  76.                 fprintf(stderr, "failed to read 2nd list\n");
  77.             } else {
  78.                 NXClose(spStream);
  79.                 NXCloseTypedStream(spTypedStream);
  80.                 printf("%d objects in new list\n", [oList2 count]);
  81.             }
  82.         }
  83.     } else
  84.         fprintf(stderr, "couldn't map in input file\n");
  85.  
  86.     for (j = 0; j < [oList2 count]; ++j) {
  87.         RCString *oString = [oList2 objectAt:j];
  88.         if (oString)
  89.             printf("added an RCString, \"%s\", 0x%x, refs %d\n",
  90.                 [oString data], [oString data], [oString references]);
  91.     }
  92.  
  93.     printf("first list %s second list\n",
  94.         [oList isEqual:oList2]? "same as" : "different from");
  95.     
  96.     [[oList freeObjects] free];
  97.     [[oList2 freeObjects] free];
  98.  
  99.     exit(0);
  100. }
  101.